package com.dangdang.controller;

import com.dangdang.entity.Book;
import com.dangdang.entity.Cart;
import com.dangdang.entity.CartItem;
import com.dangdang.service.BookService;
import com.dangdang.service.impl.BookServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/cart")
public class CartController {
    private BookService bs = new BookServiceImpl();
    @RequestMapping("/buyBook")
    public String buyBook(int bid, HttpSession session){
        Book book = bs.showOne(bid);
        Cart cart = (Cart) session.getAttribute("cart");
        if(cart==null){
            cart = new Cart();
            CartItem cartItem = new CartItem(book,1);
            cart.getBuyBooks().put(bid,cartItem);
        }else{
            Map<Integer, CartItem> buyBooks = cart.getBuyBooks();
            if(buyBooks.containsKey(bid)){
                CartItem cartItem = buyBooks.get(bid);
                cartItem.setCount(cartItem.getCount()+1);
            }else{
                CartItem cartItem = new CartItem(book,1);
                buyBooks.put(bid,cartItem);
            }

        }
        calMoney(cart);

        session.setAttribute("cart",cart);
        return "redirect:/cart/cart_list.jsp";
    }
    @RequestMapping("/delete")
    public String delete(int bid,HttpSession session){
        Cart cart = (Cart) session.getAttribute("cart");
        Book book = bs.showOne(bid);

        Map<Integer, CartItem> removeBooks = cart.getRemoveBooks();
        CartItem cartItem = new CartItem();
        cartItem.setBook(book);
        cartItem.setCount(cart.getBuyBooks().get(bid).getCount());
        cart.getBuyBooks().remove(bid);
        removeBooks.put(bid,cartItem);
        session.setAttribute("cart",cart);
        calMoney(cart);
        return "redirect:/cart/cart_list.jsp";
    }
    @RequestMapping("/recover")
    public String recover(int bid,HttpSession session){
        Cart cart = (Cart) session.getAttribute("cart");
        Book book = bs.showOne(bid);

        Map<Integer, CartItem> buyBooks = cart.getBuyBooks();
        buyBooks.put(bid,new CartItem(book,cart.getRemoveBooks().get(bid).getCount()));
        cart.getRemoveBooks().remove(bid);
        session.setAttribute("cart",cart);
        calMoney(cart);
        return "redirect:/cart/cart_list.jsp";
    }
    @RequestMapping("/update")
    public String update(int bid,int count,HttpSession session){
        Cart cart = (Cart) session.getAttribute("cart");
        cart.getBuyBooks().get(bid).setCount(count);
        session.setAttribute("cart",cart);
        calMoney(cart);
        return "redirect:/cart/cart_list.jsp";
    }

    private void calMoney(Cart cart) {
        Map<Integer, CartItem> buyBooks = cart.getBuyBooks();
        double totalPrice = 0;
        double savePrice = 0;
        Set<Map.Entry<Integer, CartItem>> entries = buyBooks.entrySet();
        for(Map.Entry entry:entries){
            Integer key =(Integer) entry.getKey();
            CartItem cartItem = (CartItem) entry.getValue();
            Book b = cartItem.getBook();
            totalPrice+= b.getDangPrice()*cartItem.getCount();
            savePrice += (b.getPrice()-b.getDangPrice())*cartItem.getCount();
        }
        cart.setTotalMoney(totalPrice);
        cart.setSaveMoney(savePrice);
    }
}
